package com.example.community.update;

import com.example.community.dao.PaytypeDao;
import com.example.community.model.Paytype;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/payTypeUpdateServlet")
public class PayTypeUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ptNo = req.getParameter("ptNo");
        PaytypeDao paytypeDao = new PaytypeDao();
        Paytype paytype = paytypeDao.fandById(Integer.parseInt(ptNo));
        req.setAttribute("paytype",paytype);
        req.getRequestDispatcher("/WEB-INF/payManager/paytypeUpdate.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String ptNo = req.getParameter("ptNo");
            String ptName = req.getParameter("ptName");

            Paytype paytype = new Paytype();
            paytype.setPtNo(Integer.parseInt(ptNo));
            paytype.setPtName(ptName);

            PaytypeDao paytypeDao = new PaytypeDao();
            Integer update = paytypeDao.update(paytype);
            if (update == 1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }catch (Exception e){
            resp.getWriter().write("false");
        }
    }
}
